﻿using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class CompanyController : BaseAuthController
    {
        CompanyBLL _companyBLL = new CompanyBLL();
        //
        // GET: /Company/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            List<object> result = new List<object>();
            PagedList<EMOA_Company> list = _companyBLL.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows));
            list.ForEach(p =>
            {
                result.Add(new CompanyModels().ToModel<EMOA_Company>(p));
            });

            return ToJson(result, list.TotalItemCount);
        }

        public JsonResult GetEntity(Guid id)
        {
            var data = new CompanyModels().ToModel<EMOA_Company>(_companyBLL.GetEntity(id));

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        //
        // POST: /Task/Create

        [HttpPost]
        public ActionResult Create(CompanyModels model)
        {
            try
            {
                EMOA_Company ep = model.ToEntity<EMOA_Company>();
                ep.CompanyID = Guid.NewGuid();
                if (_companyBLL.Add(ep))
                {
                    return Content("OK");
                }
                else
                {
                    return Content("失败");
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        //
        // POST: /Task/Edit/5

        [HttpPost]
        public ActionResult Edit(CompanyModels model)
        {
            try
            {
                EMOA_Company entity = _companyBLL.GetEntity(model.CompanyID);
                entity = model.ToEntity<EMOA_Company>(entity);
                if (_companyBLL.Update(entity))
                    return Content("OK");
                else
                    return Content("失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }


        //
        // POST: /Task/Delete/5

        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                if (_companyBLL.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }
    }
}
